package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.ActionProcessor;
import com.cynteka.cyfoman.core.Initializer;
import com.cynteka.cyfoman.core.ValueObject;
import com.cynteka.cyfoman.core.config.ActionMD;
import com.cynteka.cyfoman.core.config.ActionsMD;

import javax.sql.DataSource;
import javax.swing.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainWindow {
    private Logger logger = Logger.getLogger(MainWindow.class.getName());

    public JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Создать");

        try {
            ActionsMD actions = readActionsMD();
            final File baseFolder = (File) Context.getBeanFactory().getBean("baseFolder");
            final DataSource dataSource = (DataSource) Context.getBeanFactory().getBean("dataSource");
            for (final ActionMD action : actions) {
                JMenuItem menuItem = new JMenuItem(action.getName());
                menu.add(menuItem);
                menuItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        final ValueObject valueObject = new ValueObject();
                        Initializer initializer = (Initializer) Context.getBeanFactory().getBean("actionInitializer");
                        initializer.init(action, valueObject);
                        ActionParamDlg paramDlg = new ActionParamDlg(action, valueObject) {
                            @Override
                            protected void onOK() {
                                super.onOK();
                                ActionProcessor.processAction(action, valueObject, baseFolder, dataSource, null);
                            }
                        };
                        paramDlg.pack();
                        paramDlg.setVisible(true);
//
//						JDialog dialog = new JDialog();
//						dialog.setTitle("Введите параметры");
//						dialog.setLayout(new GridLayout(1, 1));
//						dialog.setContentPane(ParamPanelBuilder.createContentPane(action.getActionParams()));
//						dialog.setSize(600, 400);
//						dialog.pack();
//						dialog.setSize(600, dialog.getHeight());
//						dialog.setLocation(200, 200);
//						dialog.setVisible(true);

                    }
                });
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error waiting", e);
        }

        menuBar.add(menu);
        return menuBar;
    }

    protected ActionsMD readActionsMD() throws JAXBException, IOException {
        JAXBContext context = JAXBContext.newInstance(ActionsMD.class);
        FileInputStream fis = new FileInputStream(Parameters.getActionsFileName());
        Unmarshaller um = context.createUnmarshaller();
        ActionsMD res = (ActionsMD) um.unmarshal(fis);
        return res;
    }


    public Container createContentPane() {
        JPanel contentPane = new JPanel(new BorderLayout());
        return contentPane;
    }

}
